/*
 * GuiDisplayGroupAccessByNode.java
 *
 * Created on 27 July 2004, 11:54
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.access;

import com.velocityme.client.gui.exception.ExceptionWindow;
import javax.ejb.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import java.awt.Component;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.JTabbedPane;
import com.velocityme.client.gui.exception.*;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiDisplayGroupAccessByNode extends javax.swing.JPanel {
    
    private NodePK m_nodePK;
    private KeySession m_key;
    private ArrayList m_groupAccesses;
    private GroupAccessPK m_selectedGroupAccessPK;
    private GroupAccessTableModel m_modelGroupAccess;
    
    
    /** Creates new form GuiDisplayGroupAccessByNode */
    public GuiDisplayGroupAccessByNode(KeySession p_key, NodePK p_nodePK) {
        initComponents();
        m_key = p_key;
        m_nodePK = p_nodePK;
        try {
            jTextFieldNode.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, m_nodePK));
            m_groupAccesses = new ArrayList();
            Iterator i = StatelessRemoteClientSessionUtil.getSession().getGroupAccessByNodeValue(m_key, m_nodePK).iterator();
            while(i.hasNext()) {
                GroupAccessValue groupAccessValue = (GroupAccessValue)i.next();
                NodePK nodePK = groupAccessValue.getNodeValue().getPrimaryKey();
                String nodePath = StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, nodePK);
                String group = groupAccessValue.getGroupValue().getNodeValue().getName();
                String role = groupAccessValue.getRoleValue().getNodeValue().getName();
                m_modelGroupAccess.addRow(nodePath, group, role);
                m_groupAccesses.add(groupAccessValue);
            }
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jButtonDelete = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabelNode = new javax.swing.JLabel();
        jTextFieldNode = new javax.swing.JTextField();
        jLabelGroupAccesses = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_modelGroupAccess = new GroupAccessTableModel();
        jTableGroupAccess = new javax.swing.JTable(m_modelGroupAccess);

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jButtonDelete.setText("Delete");
        jButtonDelete.setEnabled(false);
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jPanel1.add(jButtonDelete);

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jPanel1.add(jButtonClose);

        add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel2.setMinimumSize(new java.awt.Dimension(0, 0));
        jLabelNode.setText("Node:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelNode, gridBagConstraints);

        jTextFieldNode.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jTextFieldNode, gridBagConstraints);

        jLabelGroupAccesses.setText("Group Accesses:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelGroupAccesses, gridBagConstraints);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(3, 120));
        jTableGroupAccess.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableGroupAccessMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jTableGroupAccess);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.33;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jScrollPane1, gridBagConstraints);

        add(jPanel2, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jTableGroupAccessMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGroupAccessMouseClicked
        // Add your handling code here:
        try {
            int row = jTableGroupAccess.getSelectedRow();
            if (row != -1) {
                m_selectedGroupAccessPK = ((GroupAccessValue)m_groupAccesses.get(row)).getPrimaryKey();
                NodePK nodePK = ((GroupAccessValue)m_groupAccesses.get(row)).getNodeValue().getPrimaryKey();
                Set setPermissions = StatelessRemoteClientSessionUtil.getSession().getAssignedPermissions(m_key, nodePK);
                jButtonDelete.setEnabled(setPermissions.contains("Delete Group Access"));
            }
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }//GEN-LAST:event_jTableGroupAccessMouseClicked

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        // Add your handling code here:
        try {
            StatelessRemoteClientSessionUtil.getSession().groupAccessDelete(m_key, m_selectedGroupAccessPK);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        getParent().remove(this);                
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        // Add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCloseActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JLabel jLabelGroupAccesses;
    private javax.swing.JLabel jLabelNode;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableGroupAccess;
    private javax.swing.JTextField jTextFieldNode;
    // End of variables declaration//GEN-END:variables
    
}
